package com.chris.design.pattern.samples.strategy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述说明
 *
 * @author Chris
 * @since 2019/8/26
 */
public class ExecutorAdapter {
    private static final Map<String, Object> MAP_CACHE = new HashMap<String, Object>();
    static {
        MAP_CACHE.put("1", "adapterA");
        MAP_CACHE.put("2", "adapterB");
        MAP_CACHE.put("3", "adapterElse");
    }

    public IExecutorInterface adapterA() {
        return new ExecutorA();
    }

    public IExecutorInterface adapterB() {
        return new ExecutorB();
    }

    /**
     * 多态
     * @param arg 参数枚举
     * @return
     */
    public IExecutorInterface invoke(String arg) {
        IExecutorInterface excute = null;
        if (this.MAP_CACHE.containsKey(arg)) {
            Class<?> c = this.getClass();
            try {
                Method m = c.getMethod(String.valueOf(MAP_CACHE.get(arg)));
                excute = (IExecutorInterface) m.invoke(this, new Object[]{});
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } else {
            throw new IllegalArgumentException("参数错误");
        }

        return excute;
    }
}
